﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Bigger.Base;
using Bigger.Framework;
using System.Data;
using System.Threading;
using Bigger.Study.Business;
using Bigger.Study.DataEntity;
using System.Xml;
using Bigger.Core.Service;
using Bigger.Core.Logging;

namespace Bigger.Study.ConsoleApp
{
    public class BiggerBaseExample : IExample
    {
        public void Execute()
        {
            Register();

            //Select();

            //Insert();

            //Update();

            //Delete();

            ExecuteApi();
        }

        void Register()
        {
            //初始化服务
            UnityConfiguration.UnityConfig.InitialConfig();
            BaseConfiguration.RegisterWriteSqlLog((sender, e) =>
            {
                ILogService objLogService = LogConfiguration.LogServiceProvider.CreateLogService();
                objLogService.WriteSqlLog(e.DbConnection, e.SqlString, e.DbParameters);
            });
        }

        void Select()
        {
            //BizCourse objBizCourse = new BizCourse();

            //Course objCourse1 = objBizCourse.GetRecord(Guid.Parse("62f15dda-bb97-422b-b3ad-88f163c72376"));
            //Console.WriteLine(objCourse1.CourseTitle);

            //List<Course> objList1 = objBizCourse.GetList("StartTime>@StartTime", new { StartTime = DateTime.Parse("2015-12-07 11:41:52") });
            //foreach (Course objCourse in objList1)
            //{
            //    Console.WriteLine(objCourse.CourseTitle);
            //}

            //List<Course> objList2 = objBizCourse.GetPageList(0, 10, "StartTime>@StartTime", new { StartTime = DateTime.Parse("2015-12-07 11:41:52") });
            //foreach (Course objCourse in objList2)
            //{
            //    Console.WriteLine(objCourse.CourseTitle);
            //}

            //List<Course> objList3 = objBizCourse.GetLimitList(0, 10, "StartTime>@StartTime", new { StartTime = DateTime.Parse("2015-12-07 11:41:52") });
            //foreach (Course objCourse in objList3)
            //{
            //    Console.WriteLine(objCourse.CourseTitle);
            //}

            //DataTable objDataTable1 = objBizCourse.GetDataTable("StartTime>@StartTime", new { StartTime = DateTime.Parse("2015-12-07 11:41:52") });
            //foreach (DataRow objDataRow in objDataTable1.Rows)
            //{
            //    Console.WriteLine(objDataRow["CourseTitle"]);
            //}

            //DataSet objDataSet1 = objBizCourse.GetPageDataSet(0, 10, true);
            //if (objDataSet1.IsNoNull())
            //{
            //    foreach (DataRow objDataRow in objDataSet1.Tables[0].Rows)
            //    {
            //        Console.WriteLine(objDataRow["CourseTitle"]);
            //    }
            //    foreach (DataRow objDataRow in objDataSet1.Tables[1].Rows)
            //    {
            //        Console.WriteLine(objDataRow["RecordCount"]);
            //    }
            //}
        }

        void Insert()
        {
            //BizCourse objBizCourse = new BizCourse();

            //Course objCourse1 = InitialCourse();
            //objBizCourse.Add(objCourse1);

            //List<Course> objList1 = new List<Course>();
            //for (int i = 0; i < 3; i++)
            //{
            //    Course objCourse2 = InitialCourse();
            //    objCourse2.SetState(EntityStatus.Added);
            //    objList1.Add(objCourse2);
            //}
            //objBizCourse.Batch(objList1);

        }

        void Update()
        {
            //BizCourse objBizCourse = new BizCourse();

            //objBizCourse.Update("CourseTitle=@CourseTitle", "CourseId=@CourseId", new { CourseTitle = "***课程***" + DateTime.Now.ConvertToStamp(), CourseId = "a7ff3e4c-dced-456d-aa4e-71a80cce8419" });

            //List<Course> objList1 = objBizCourse.GetList("");
            //foreach (Course objCourse1 in objList1)
            //{
            //    objCourse1.Status = 1;
            //    objCourse1.SetState(EntityStatus.Modified);
            //}
            //objBizCourse.Batch(objList1);
        }

        void Delete()
        {
            //BizCourse objBizCourse = new BizCourse();

            //objBizCourse.Delete(Guid.Parse("d8f79040-c85b-4881-aa1a-4ffeb5b2bd84"));

            //List<Course> objList1 = objBizCourse.GetList("CourseTitle like @CourseTitle", new { CourseTitle = "课程标题%" });
            //foreach (Course objCourse in objList1)
            //{
            //    objCourse.SetState(EntityStatus.Deleted);
            //}
            //objBizCourse.Batch(objList1);
        }

        void ExecuteApi()
        {
            //BizBase objBizBase = new BizBase();

            ////ApiContext objApiContext1 = objBizBase.CreateApiContext("Bigger_Course", "ApiGroup_Course", "Bigger_CoursePage", new { PageIndex = 0, PageSize = 2 });
            //object objResult1 = objBizBase.ExecuteApi("Bigger_Course", "ApiGroup_Course", "Bigger_CoursePage", new { PageIndex = 0, PageSize = 2 });
            //Console.WriteLine(objResult1.JsonSerialize());

            //List<Dictionary<string, object>> objList1 = objBizBase.GetListDictionary("Bigger_Course", "ApiGroup_Course", "Bigger_CoursePage", new { PageIndex = 1, PageSize = 2 });
            //foreach (Dictionary<string, object> objDict in objList1)
            //{
            //    Console.WriteLine(objDict.GetDictValue<string>("CourseTitle"));
            //}

            //DataSet objDataSet1 = objBizBase.ExecuteDataSet("Bigger_Course", "ApiGroup_Course", "Bigger_CourseDataSet", new { CourseId = "62F15DDA-BB97-422B-B3AD-88F163C72376" });
            //if (objDataSet1.Tables.Count == 2)
            //{
            //    //foreach (DataRow objDataRow in objDataSet1.Tables[0].Rows)
            //    //{
            //    //    Console.WriteLine(objDataRow["CourseTitle"]);
            //    //}
            //    //foreach (DataRow objDataRow in objDataSet1.Tables[1].Rows)
            //    //{
            //    //    Console.WriteLine(objDataRow["ChapterTitle"]);
            //    //}

            //    //List<List<Dictionary<string, object>>> objList2= objDataSet1.ToListListDictionary();
            //    //foreach (Dictionary<string, object> objDict in objList2[0])
            //    //{
            //    //    Console.WriteLine(objDict.GetDictValue<string>("CourseTitle"));
            //    //}
            //    //foreach (Dictionary<string, object> objDict in objList2[1])
            //    //{
            //    //    Console.WriteLine(objDict.GetDictValue<string>("ChapterTitle"));
            //    //}
            //}

            //DataTable objDataTable1 = objBizBase.ExecuteDataTable("Bigger_Course", "ApiGroup_Course", "Bigger_CourseDataTable", new { CourseId = "62F15DDA-BB97-422B-B3AD-88F163C72376" });
            //foreach (DataRow objDataRow in objDataTable1.Rows)
            //{
            //    Console.WriteLine("{0}:{1}", objDataRow.GetDataRowValue("CourseTitle"), objDataRow.GetDataRowValue("ChapterTitle"));
            //}

            //int totalCount1 = objBizBase.ExecuteScalar<int>("Bigger_Course", "ApiGroup_Course", "Bigger_CourseTotalCount");
            //Console.WriteLine(totalCount1);

            //Guid courseId = objBizBase.ExecuteScalar<Guid>("Bigger_Course", "ApiGroup_Course", "Bigger_CourseInsert");
            //Console.WriteLine(courseId.ToString());

            //objBizBase.ExecuteTransaction("Bigger_Course", "ApiGroup_Course", "Bigger_CourseTransaction");

            //Course objCourse = InitialCourse();
            //Console.WriteLine(objBizBase.ExecuteNonQuery("Bigger_Course", "ApiGroup_Course", "Bigger_CourseInsertByParameters", objCourse));

            //Console.WriteLine(objBizBase.ExecuteNonQuery("Bigger_Course", "ApiGroup_Course", "Bigger_CourseInsertByParameters", new
            //{
            //    CourseTitle = "课程标题1" + DateTime.Now.ConvertToStamp(),
            //    CourseTag = "学习,兴趣",
            //    CoursePrice = "20$",
            //    CourseTime = "2",
            //    DetailAddress = "火星" + new Random().Next(0, 100),
            //    StartTime = DateTime.Now.AddHours(-1),
            //    EndTime = DateTime.Now,
            //    MaxPic = "",
            //    MidPic = "",
            //    MinPic = "",
            //    PictureData = "",
            //    StudentCount = new Random().Next(0, 100),
            //    Description = "火星救援" + new Random().Next(0, 100),
            //    Content = "<p>火星撞地球" + new Random().Next(0, 100) + "</p>",
            //    CreateTime = DateTime.Now,
            //    PublishTime = DateTime.Now.AddHours(1),
            //    Status = 1
            //}));

            //DataRow objDataRow1 = objBizBase.ExecuteDataRow("Bigger_Course", "ApiGroup_Course", "Bigger_CourseDataRow", new { CourseId = "62F15DDA-BB97-422B-B3AD-88F163C72376" });
            //Console.WriteLine(objDataRow1.GetDataRowValue<string>("CourseTitle"));

            //Dictionary<string, object> objDict1 = objBizBase.GetRecordDictionary("Bigger_Course", "ApiGroup_Course", "Bigger_CourseRecord", new { CourseId = "62F15DDA-BB97-422B-B3AD-88F163C72376" });
            //Console.WriteLine(objDict1.GetDictValue<string>("CourseTitle"));

            //List<Dictionary<string, object>> objList3 = objBizBase.GetListDictionary("Bigger_Course", "ApiGroup_Course", "Bigger_CourseListBySort1", null, new { SortExpression = "PublishTime desc" });
            //foreach (Dictionary<string, object> objDict in objList3)
            //{
            //    Console.WriteLine(objDict.GetDictValue<string>("CourseTitle"));
            //}

            //List<Dictionary<string, object>> objList3 = objBizBase.GetListDictionary("Bigger_Course", "ApiGroup_Course", "Bigger_CourseListBySort2", null, new { SortType = 1 });
            //foreach (Dictionary<string, object> objDict in objList3)
            //{
            //    Console.WriteLine(objDict.GetDictValue<string>("CourseTitle"));
            //}

            //ApiContext objApiContext2 = objBizBase.CreateApiContext("Bigger_Course", "ApiGroup_Course", "Bigger_CourseListBySort2", null, new { SortType = 1 });
            //Console.ReadKey();

            string xmlpath = ConfigHelper.GetValue("Bigger_Course", "", "SqlConfig");
            if (xmlpath.IndexOf(":") < 0)
            {
                if (AppDomain.CurrentDomain.BaseDirectory.Last() == '\\')
                {
                    xmlpath = AppDomain.CurrentDomain.BaseDirectory + xmlpath;
                }
                else
                {
                    xmlpath = AppDomain.CurrentDomain.BaseDirectory + "\\" + xmlpath;
                }
            }
            XmlDocument objXmlDocument1 = xmlpath.CreateXmlDocument();
            XmlDocument objXmlDocument2 = xmlpath.CreateXmlDocument();
            Console.WriteLine(object.ReferenceEquals(objXmlDocument1, objXmlDocument2));

            //在这里修改xml文件
            Console.ReadKey();
            //修改后
            XmlDocument objXmlDocument3 = xmlpath.CreateXmlDocument();
            Console.WriteLine(object.ReferenceEquals(objXmlDocument2, objXmlDocument3));
            XmlDocument objXmlDocument4 = xmlpath.CreateXmlDocument();
            Console.WriteLine(object.ReferenceEquals(objXmlDocument3, objXmlDocument4));

            ////修改xml，测试数据是否更新
            //Dictionary<string, object> objDict2 = null;
            //for (int i = 0; i < 100; i++)
            //{
            //    objDict2 = objBizBase.GetRecordDictionary("Bigger_Course", "ApiGroup_Course", "Bigger_TestXmlCache");
            //    foreach (string key in objDict2.Keys)
            //    {
            //        Console.WriteLine(objDict2[key]);
            //    }
            //    System.Threading.Thread.Sleep(1000);
            //}

        }

        Course InitialCourse()
        {
            Course objTarget = new Course();
            //课程标题
            objTarget.CourseTitle = "课程标题1" + DateTime.Now.ConvertToStamp();
            //课程标签
            objTarget.CourseTag = "学习,兴趣";
            //课程价格
            objTarget.CoursePrice = "20$";
            //课程课时
            objTarget.CourseTime = "2";
            //详细地址
            objTarget.DetailAddress = "火星" + new Random().Next(0, 100);
            //开始时间
            objTarget.StartTime = DateTime.Now.AddHours(-1);
            //结束时间
            objTarget.EndTime = DateTime.Now;
            //课程大图
            objTarget.MaxPic = null;
            //课程中图
            objTarget.MidPic = null;
            //课程小图
            objTarget.MinPic = null;
            //照片资料
            objTarget.PictureData = null;
            //学员人数
            objTarget.StudentCount = new Random().Next(0, 100);
            //课程描述
            objTarget.Description = "火星救援" + new Random().Next(0, 100);
            //课程内容
            objTarget.Content = "<p>火星撞地球" + new Random().Next(0, 100) + "</p>";
            //创建时间
            objTarget.CreateTime = DateTime.Now;
            //PublishTime
            objTarget.PublishTime = DateTime.Now.AddHours(1);
            //课程状态：0,、未审核，1、审核通过，2、审核不通过
            objTarget.Status = 1;
            return objTarget;
        }
    }
}
